package com.intercrptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        //放行:判断什么情况下登录
        //用户登录过就放行
          if (session.getAttribute("userLogInfo")!=null){
           return true;
        }
          if (request.getRequestURI().contains("goLogin")){
              return true;
          }
          //用户首次登录的情况,需要放行登录页面
        if (request.getRequestURI().contains("login")){
            return true;
        }

        //判断什么情况下没有登录,没有登录就拦截
        request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request,response);
          return false;
    }
}
